package com.wisewin.pose.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dypnsapi.model.v20170525.GetMobileRequest;
import com.aliyuncs.dypnsapi.model.v20170525.GetMobileResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AliyunUtil {

    final static Logger logger = LoggerFactory.getLogger(AliyunUtil.class);

    private static final String accessKeyId = "LTAI4GARM7SLR52v6ApSqKyY";

    private static final String accessKeySecret = "PpFJzMbfrSvm2z5OJ8UZKSpwQTTZVT";

    // 目前只有"cn-hangzhou"这个region可用，不要使用填写其他region的值
    public static final String REGION_CN_HANGZHOU = "cn-hangzhou";

    // 一键登录获取手机号
    public static String getMobile(String accessToken) {
        DefaultProfile profile = DefaultProfile.getProfile(REGION_CN_HANGZHOU, accessKeyId, accessKeySecret);
        IAcsClient client =new DefaultAcsClient(profile);
        GetMobileRequest request = new GetMobileRequest();
        request.setAccessToken(accessToken);//app端传过来的，需要用户授权拿到
        request.setRegionId(REGION_CN_HANGZHOU);
        String phone = null;
        try {
            GetMobileResponse response = client.getAcsResponse(request);
            if("OK".equals(response.getCode())) {
                phone = response.getGetMobileResultDTO().getMobile();
            };
            logger.info(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            logger.info("ErrCode:" + e.getErrCode());
            logger.info("ErrMsg:" + e.getErrMsg());
            logger.info("RequestId:" + e.getRequestId());
        }
        return phone;
    }

}
